<template>
  <ScrollPage>
    <view class="page-child">
      <view v-for="item in childLists" :key="item.id" class="child-item">
        <view class="col-t">
          <view class="col-l">
            {{ item.name }}
          </view>
          <view class="col-r" @click="remove(item)">
            删除
          </view>
        </view>
        <view class="col-b">
          <view class="col-l">
            积分
            <view class="count">
              {{ item.score || 0 }}
            </view>
          </view>
        </view>
      </view>
      <view class="empty-container" />
    </view>
    <template #bottom>
      <button class="add-btn" @click="show = true">
        添加子女
      </button>
    </template>
  </ScrollPage>
  <SheetCom v-model="show" title="添加子女" @submit="submit" @close="reset">
    <FormItem title="姓名">
      <wd-input v-model="form.name" placeholder="请输入" />
    </FormItem>
  </SheetCom>
</template>

<script lang="ts" setup>
import type { IChildInfo } from '@/api/child'
import { deepClone } from '@alova/shared'
import { storeToRefs } from 'pinia'
import { addChild, removeChild } from '@/api/child'
import FormItem from '@/components/FormItem.vue'
import SheetCom from '@/components/Sheet.vue'
import ScrollPage from '@/layouts/ScrollPage.vue'
import { useChildStore } from '@/store/child'

defineOptions({
  name: 'Child',
})

const defaultData: IChildInfo = {
  name: '',
}

const show = ref(false)
const form = ref(deepClone(defaultData))

definePage({
  style: {
    // 'custom' 表示开启自定义导航栏，默认 'default'
    navigationStyle: 'default',
    navigationBarTitleText: '我的子女',
    backgroundColor: '#fafafa',
  },
})
// #region Props
// #endregion
// #region Emits
// #endregion
// #region Hooks
const childStore = useChildStore()
const { childLists } = storeToRefs(childStore)
// #endregion
// #region Computed
// #endregion
// #region Watch
// #endregion
// #region Event
// #endregion
// #region Function
function refresh() {
  setTimeout(() => {
    childStore.fetchChildList()
  }, 500)
}

function reset() {
  form.value = deepClone(defaultData)
  show.value = false
}

function remove(item: IChildInfo) {
  if (childLists.value.length < 2) {
    return uni.showToast({
      title: '至少保留一个孩子',
      icon: 'none',
    })
  }
  uni.showModal({
    content: `确认删除${item.name}?`,
    success: (res) => {
      if (res.confirm) {
        removeChild({ id: item.id }).then(() => {
          refresh()
        })
      }
    },
  })
}
function submit(cb: () => void) {
  if (!form.value.name) {
    cb()
    return uni.showToast({
      title: '请填写信息',
      icon: 'none',
    })
  }

  addChild({
    ...form.value,
  }).then(() => {
    refresh()
    reset()
  }).finally(() => {
    cb()
  })
}
// #endregion
// #region Life Cycle
// #endregion
// #region Expose
// #endregion
</script>

<style lang="scss" scoped>
.page-child {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 0 20px;
  .child-item {
    background: var(--card-border-color);
    border-radius: 16px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    border: 1px solid var(--card-border-gary-color);
    box-sizing: border-box;
    padding: 10px;
    margin-top: 20px;
    .col-t {
      margin-bottom: 20px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .col-l {
        font-size: 16px;
        color: var(--main-color);
        font-weight: 600;
      }
      .col-r {
        height: 30px;
        color: var(--color-red);
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .col-b {
      background: var(--bg-color);
      height: 40px;
      border-radius: 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 10px;
      font-size: 14px;
      color: var(--text-color);
      position: relative;
      .col-l {
        display: flex;
        align-items: center;
        gap: 10px;
        .count {
          color: var(--light-color);
        }
      }
      &::after {
        content: '';
        display: flex;
        background: url('https://mp-faaaa8a0-2641-45b8-8dbc-bcb0d1463eb4.cdn.bspapp.com/app/icon-more-gary.png')
          no-repeat;
        background-size: cover;
        width: 24px;
        height: 24px;
        position: absolute;
        right: 0;
      }
    }
  }
  .empty-container {
    height: 155px;
    flex: 0 0 auto;
  }
}
.add-btn {
  height: 60px;
  background: var(--button-color);
  margin: 20px 20px 0 20px;
  border-radius: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
